import json
import os
from xml.dom.minidom import parse
import xml.dom.minidom
path = 'C:/Users/何涛/Downloads/train_e2e.txt'

def paixu(p):
    p = sorted(p, key=lambda point: point[1])
    sorts = []
    if p[0][0]<p[1][0]:
        sorts.append(p[0])
        sorts.append(p[1])
    else:
        sorts.append(p[1])
        sorts.append(p[0])
    if p[2][0]<p[3][0]:
        sorts.append(p[3])
        sorts.append(p[2])
    else:
        sorts.append(p[2])
        sorts.append(p[3])
    return sorts



def point_4():
    f = open(path, "r").readlines()
    file = open("train_e2e.txt", "w", encoding='utf-8')
    for i in f:
        if len(i) > 10:
            pic, content = i.split("\t")
            j = json.loads(content)
            p = j[0]['points']

            #sort_by = sorted(p, key=lambda point: point[1])

            j[0]['points'] = paixu(sort_by)

            file.write(pic + '\t' + json.dumps(j) + "\n")


def get_four_point(xmin,ymin,xmax,ymax):
    return [[xmin,ymin],[xmax,ymin],[xmax,ymax],[xmin,ymax]]

def process_xml(xmlpath,imgpath):
    #[{"transcription": "浙JS75C2", "points": [[705, 171], [706, 201], [803, 206], [807, 175]], "difficult": false}]
    list_xml = os.listdir(xmlpath)
    dic = 0
    handwriting = ['handwriting','autograph']
    fi = open('det_handwritting_train.txt', "w", encoding='utf-8')
    for i in range(len(list_xml)):
        if os.path.exists(os.path.join(imgpath,list_xml[i].split(".")[0]+".jpg")):
            biaozhu = []
            zidian = {}

            # print(list_xml[i])
            if os.path.exists(xmlpath + "/" + list_xml[i]):

                dom = xml.dom.minidom.parse(xmlpath + "/" + list_xml[i])
                # 得到文档元素对象
                root = dom.documentElement
                if len(root.getElementsByTagName('object')) > 0:
                    object = root.getElementsByTagName('object')
                    for ob in range(len(object)):
                        zidian = {}
                        name = object[ob].getElementsByTagName('name')[0].childNodes[0].data
                        if name in handwriting:
                            bndbox = object[ob].getElementsByTagName('bndbox')[0]
                            xmin = bndbox.getElementsByTagName('xmin')[0].childNodes[0].data
                            ymin = bndbox.getElementsByTagName('ymin')[0].childNodes[0].data
                            xmax = bndbox.getElementsByTagName('xmax')[0].childNodes[0].data
                            ymax = bndbox.getElementsByTagName('ymax')[0].childNodes[0].data
                            zidian.update(transcription='111')
                            zidian.update(points=get_four_point(xmin, ymin, xmax, ymax))
                            biaozhu.append(zidian)
                    if len(biaozhu) > 0:
                        fi.write(os.path.join(imgpath, list_xml[i].split('.')[0] + '.jpg') + "\t" + json.dumps(
                            biaozhu) + "\n")


#f = open("C:/Users/何涛/Downloads/total_text/train/train.txt","r").readlines()
#print(f[0])
process_xml('D:/BaiduNetdiskDownload/报告单标注图片4.14/报告单xml','D:/BaiduNetdiskDownload/报告单标注图片4.14/报告单源图片')






